package test;

/**
 * @Description
 * @Author Firenut
 * @Date 2023-08-21 11:54
 */
public class DumpSort {
    void heapSort(int[] nums) {
        for (int i = nums.length / 2 - 1; i >= 0; i--) {
            heapAdjust(nums, i, nums.length);
        }
        for (int i = nums.length - 1; i > 0; i--) {
            swap(nums, 0, i);
            heapAdjust(nums, 0, i);
        }
    }


    void heapAdjust(int[] nums, int i, int len) {
        int temp = nums[i];
        for (int j = 2 * i + 1; j < len; j = j * 2 + 1) {
            if (j + 1 < len && nums[j] < nums[j + 1]) {
                j = j + 1;
            }
            if (temp > nums[j]) break;
            nums[i] = nums[j];
            i = j;
        }
        nums[i] = temp;
    }


    void swap(int[] nums, int i, int j) {
        int temp = nums[j];
        nums[j] = nums[i];
        nums[i] = temp;
    }
}
